<?php

namespace app\admin\controller\ykjp\summary;

use app\common\controller\Backend;
use think\Db;
use app\admin\model\ykjp\product\Product;
use app\admin\model\ykjp\information\basisinfo\Customerinfo;

/**
 * 销售汇总
 *
 * @icon fa fa-circle-o
 */
class Sell extends Backend {

    protected $model = null;
    protected $searchFields = "name";
    protected $distinguish = true;
    protected $wherename = "";

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\ykjp\sell\deliveryPro;
    }

    /**
     * 查看
     */
    public function index() {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $this->wherename = "delivery.firmid";
            // var_dump(11);exit;
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['delivery'])
                    ->where("delivery.status_list", "6")
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['delivery'])
                    ->where($where)
                    ->where("delivery.status_list", "6")
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            // var_dump($list);exit;
            $productModel = new Product;
            $custModel = new Customerinfo;
            $nums = 0;
            $money = 0;
            foreach ($list as $row) {
                $nums += $row['nums'];
                $money += $row['subtotal'];
                $row['proname'] = "";
                $row['cunsName'] = "";
                $proName = $productModel->where("id", $row['product_id'])->Field("name")->find();
                if ($proName) {
                    $row['proname'] = $proName["name"];
                }
                $cunsInfo = $custModel->where("id", $row['delivery']['customer_info_id'])->find();
                if ($cunsInfo) {
                    $row['cunsName'] = $cunsInfo["name"];
                }
                $row->getRelation('delivery')->visible(['id', "code", "createtime"]);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list, "extend" => array("totalNums" => $nums, "totalMoney" => $money));
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 产品信息
     *
     */
    public function product() {
        $this->model = new Product;
        return parent::selectpage();
    }

    /**
     * 客户信息
     *
     */
    public function getCusinfo() {
        $this->model = new Customerinfo;
        return parent::selectpage();
    }

}
